const {
    Paragraph,
    TextRun,
    AlignmentType,
    TabStopType,
    TabStopPosition,
    HeadingLevel,
    TableOfContents,
    StyleLevel
} = require("docx");

/**
 * 生成可跳转的目录页面
 * @param {Array} configSections - 配置的章节数据
 * @returns {Array} - 包含目录的段落数组
 */
function generateTableOfContents(configSections) {
    const tocElements = [];
    
    // 添加"目录"标题
    tocElements.push(
        new Paragraph({
            alignment: AlignmentType.CENTER,
            spacing: { before: 400, after: 400 },
            children: [
                new TextRun({
                    text: "目录",
                    font: "SimSun",
                    size: 32,
                    bold: true,
                })
            ]
        })
    );

    // 添加Word原生目录，基于标题样式自动生成可跳转的目录
    tocElements.push(
        new TableOfContents("目录", {
            hyperlink: true,  // 启用超链接跳转
            headingStyleRange: "1-2",  // 包含1级和2级标题
            stylesWithLevels: [
                new StyleLevel("Heading1", 1),  // 1级标题对应目录1级
                new StyleLevel("Heading2", 2),  // 2级标题对应目录2级
            ],
        })
    );

    return tocElements;
}

module.exports = {
    generateTableOfContents
};